﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
using System.Windows.Media;
namespace WPFExcelReport
{
    public partial class TVA30 
    {
        #region ==IsManual===========
        public static readonly DependencyProperty IsManualProperty = DependencyProperty.Register("IsManual",
            typeof(bool), typeof(TVA30),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsManualChanged));
        public bool IsManual
        {
            get
            {
                return (bool)GetValue(IsManualProperty);
            }
            set
            {
                SetValue(IsManualProperty, value);

            }
        }
        private static void OnIsManualChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TVA30;
            if (src != null)
            {
                src.IsManual = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsGSSave===========
        public static readonly DependencyProperty IsGSSaveProperty = DependencyProperty.Register("IsGSSave",
            typeof(bool), typeof(TVA30),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsGSSaveChanged));
        public bool IsGSSave
        {
            get
            {
                return (bool)GetValue(IsGSSaveProperty);
            }
            set
            {
                SetValue(IsGSSaveProperty, value);

            }
        }
        private static void OnIsGSSaveChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TVA30;
            if (src != null)
            {
                src.IsGSSave = (bool)e.NewValue;
            }
        }
        #endregion

        public TVA30()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVAKDBEZ();
            this.DataContext = MV;
            MESSAGES += "VAKDFARBECHG;";
            InitializeComponent();
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
            FARBE = "*";
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwVAK = dwMaster;
            if (dwMaster == null)
            {
                KEY = "";
                TXTISIND = "";
            }
            else
            {
                KEY = dwMaster["VAKBKOLL"].ToString() + dwMaster["VAKBART"].ToString() + dwMaster["VAKBFARBE"].ToString() + dwMaster["VAKBKUNDNR"].ToString().Trim().PadRight(8);
                CR = dwMaster["CR"].ToString();
                CG = dwMaster["CG"].ToString();
                CB = dwMaster["CB"].ToString();
                CGRP = dwMaster["CGRP"].ToString();
                VAFBEZ = dwMaster["VAFBEZ"].ToString();
                TXTISIND = (dwMaster["VAKBKOLL"].ToString() + dwMaster["VAKBART"].ToString() + dwMaster["VAKBKUNDNR"].ToString().Trim().PadRight(8) + dwMaster["VAKBFARBE"].ToString()).PadRight(60);
            }
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            
            TXTISIND = (dwMaster["VAKBKOLL"].ToString() + dwMaster["VAKBART"].ToString() + dwMaster["VAKBKUNDNR"].ToString().Trim().PadRight(8) + dwMaster["VAKBFARBE"].ToString()).PadRight(60);
            base.EditCommand(sender, e);
        } 
        public override void OnCGRPChg(string value)
        {
            if (!IsEdit)
                return;
            if (dwVAK == null)
                return;
            dwVAK["CGRP"] = CGRP;
            base.OnCGRPChg(value);
        }

        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            try
            {
                base.OnWorkflowIDChange(e);
                MessageInfo msg = e.Info;
                string task = msg.MSGTYPE;
                switch (task)
                { 
                    case "VAKDFARBECHG":
                        VAKDFARBECHG(msg);
                        break;

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void LOADBYAUF()
        {
            IsManual = false;
            base.LOADBYAUF();
            TBMain.Clear();
            if (ORDERNUM.Length < 47)
                return;
            dwData = TBMain.NewRow();
            dwData["VAKBMANDANT"] = app.Mandant;
            dwData["VAKBKOLL"] = ORDERNUM.Substring(0, 5);
            dwData["VAKBART"] = ORDERNUM.Substring(5, 24);
            dwData["VAKBFARBE"] = ORDERNUM.Substring(29, 10);
            dwData["VAKBKUNDNR"] = ORDERNUM.Substring(39, 8);
            SResult =TMV.QVAK06(dwData);
            MergeData(SResult, true); 
        }
         
        public override void Save()
        {
           
            try
            {
                if (dwVAK == null)
                    return; 
                dwVAK["VAKBFARBE"] = Helper.Left(dwVAK["VAKBFARBE"].ToString().PadRight(10), 10);
                if (dwVAK["VAKBART"].ToString().Trim().Length == 0)
                {
                    dwVAK["VAKBART"] = Helper.Left(dwVAK["VAKBFARBE"].ToString().PadRight(24), 24);
                }
                string color = dwVAK["VAKBFARBE"].ToString().PadRight(30);
                string art = dwVAK["VAKBART"].ToString().PadRight(60);
                string des = dwVAK["VAFBEZ"].ToString().PadRight(50);
                string cr = Helper.Right("000" + dwVAK["CR"].ToString(), 3);
                string cg = Helper.Right("000" + dwVAK["CG"].ToString(), 3);
                string cb = Helper.Right("000" + dwVAK["CB"].ToString(), 3);
                string cgrp = Helper.Right("000" + dwVAK["CGRP"].ToString(), 1);

                #region=========SD0014=========
                //这个地方若是看到乱码，就正确了
                string kdcolor = Helper.Left((dwVAK["VAKBKDFARBNR"].ToString().Trim() + dwVAK["VAKBKDARTBEZ"].ToString().Trim()).PadRight(50), 50);

                DataRow dwSD = VSDDAT.SD22.TBMain.NewRow();
                VSDDAT.SD22.SetUpNewRow(ref dwSD, "0014");
                dwSD["SDSA"] = "0014";
                dwSD["SDKEY"] = "00" + Helper.Left(color.PadRight(21), 21) + "V";
                dwSD["SDDATEN"] = "A" + kdcolor.Substring(0, 40) + cr + cg + cb + cgrp + des.Substring(0, 10) + "00000000                                                                  " + app.User + MV.date + "        000000000000*";

                VSDDAT.SD22.SaveSDDAT(dwSD, app.company);
                dwSD["SDKEY"] = "00" + Helper.Left(color.PadRight(21), 21) + "E";
                VSDDAT.SD22.SaveSDDAT(dwSD, app.company);
                dwSD["SDKEY"] = "00" + Helper.Left(color.PadRight(21), 21) + " ";
                VSDDAT.SD22.SaveSDDAT(dwSD, app.company);
                #endregion

                #region=========VVAKDBEZ=========
                //string day = "", month = "", year = "";

                dwVAK["VAKBAENDBEN"] = app.User;
                dwVAK["VAKBAENDDAT"] = Helper.Right("00" + MV.day, 2) + Helper.Right("00" + MV.month, 2) + Helper.Right(MV.year, 2);
                dwVAK["VAKBKDFARBNR"] = Helpers.Helper.Left(dwVAK["VAKBKDFARBNR"].ToString().PadRight(29),29);
                dwVAK["VAFBEZ"] = Helpers.Helper.Left(dwVAK["VAFBEZ"].ToString().PadRight(20), 20);
                SResult = TMV.QueryVAKDBEZ(dwVAK, 1);
                if (SResult.Tables[0].Rows.Count == 0)
                {
                    TMV.InsertVAKDBEZ(dwVAK, app.company);
                }
                TMV.UpdateVAKDBEZ(dwVAK, app.company, 0);
                #endregion

                #region=========VAFARBEN=========
                DataRow dwVF = VVAFARBEN.Instance.TBMain.NewRow();
                VVAFARBEN.Instance.SetUpNewRow(ref dwVF, null);
                dwVF["VAFKOLL"] = dwVAK["VAKBKOLL"];
                dwVF["VAFART"] = dwVAK["VAKBART"];
                dwVF["VAFFARBE"] = dwVAK["VAKBFARBE"];
                dwVF["VAFBEZ"] = Helpers.Helper.Left(dwVAK["VAFBEZ"].ToString().PadRight(20), 20);
                dwVF["VAFFARBGRP"] = cgrp;
                dwVF["VAFAENDBEN"] = app.User;
                dwVF["VAFAENDDAT"] = MV.date;
                VVAFARBEN.Instance.UpdateVAFARBEN(dwVF, app.company, 6);
                #endregion 
                cGS04.IsGSSave = false;
              
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
        }

        public override void OnSave()
        {
            if (!IsSaved)
                return;
           
            Save();
            IsManual = false;           
            KEY = "";
            KEY = currentkey;
            ISAVE = false;
            base.OnSave();
        }
        public string currentkey
        {
            get
            {
                if (dwMaster == null) return "";
                return dwMaster["VAKBKOLL"].ToString() + dwMaster["VAKBART"].ToString() + dwMaster["VAKBFARBE"].ToString() + dwMaster["VAKBKUNDNR"].ToString();
            }
        }
        public void VAKDFARBECHG(MessageInfo msg)
        {

            if (dwMaster== null)
                return;
            dwMaster["VAKBFARBE"] = msg.P2;
           

            /*
            dwData = TBMain.NewRow();
            dwData["VAKBMANDANT"] = app.Mandant;
            dwData["VAKBKOLL"] = msg.KOLL;
            dwData["VAKBART"] = msg.ART;
            dwData["VAKBFARBE"] = msg.P2;
            dwData["VAKBKUNDNR"] = msg.P1;
            SResult = ((VVAKDBEZ)MV).QueryVAKDBEZ(dwData, 1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row.ItemArray = dwMaster.ItemArray;
                row["VAKBFARBE"] = msg.P2;
            }
            MergeData(SResult, true, true);
            */
           
        }

        public void chackcolorid(object sender, RoutedEventArgs e)
        {
            try
            {
                string colorid = txtFARBE.Text.ToString().Trim();
                txtFARBE.Foreground = Brushes.Black;
                if (colorid.Length > 10)
                {
                    MessageBox.Show("色号大于10位，请手工调整色号控制在10位之内！");
                    txtFARBE.Foreground = Brushes.Red;
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void ManualColorCommand(object sender, RoutedEventArgs e)
        {
            IsManual = true;
        }
    }
}
